package com.water.service.Impl;

import com.water.constant.JwtClaimsConstant;
import com.water.context.BaseContext;
import com.water.dto.UserLoginDto;

import com.water.dto.UserRegisterDto;
import com.water.entity.User;
import com.water.exception.ParameterException;
import com.water.json.JacksonObjectMapper;
import com.water.mapper.UserMapper;
import com.water.properties.JwtProperties;
import com.water.result.Result;
import com.water.service.UserService;
import com.water.utils.JwtUtil;
import com.water.vo.UserLoginVo;
import com.water.vo.UserUpVo;
import com.water.vo.UserVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: city_water
 * @description:
 * @author: lydms
 * @create: 2024-05-09 23:08
 **/

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 更新用户信息
     *
     * @param userUpVo
     */
    @Override
    public void update(UserUpVo userUpVo) {
        userMapper.update(userUpVo);
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @Override
    public UserVo getUserInfo() {

        Long UserId = BaseContext.getCurrentId();
        //判断用户是否登录
        if (UserId == null) {
            throw new ParameterException("用户未登录");
        }
       User user = userMapper.selectByUserId(UserId);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);

        return userVo;
    }


    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation(value = "退出")
    public Result<String> logout() {
        return Result.success();
    }


    /**
     * 用户注册
     *
     * @param userRegisterDto
     */
    public void register(UserRegisterDto userRegisterDto) {
        //判断参数是否为空
        if (userRegisterDto == null || userRegisterDto.getPhone() == null || userRegisterDto.getName() == null || userRegisterDto.getPassword() == null) {
            throw new ParameterException("参数不能为空");
        }
        if (userRegisterDto.getType() < 1 || userRegisterDto.getType() > 3) {
            throw new ParameterException("类型不能为空");
        }
        //判断手机号是不是11位数
        if (userRegisterDto.getPhone().length() != 11) {
            throw new ParameterException("手机号必须是11位");
        }

        //判断手机号是否存在
        String phone = userRegisterDto.getPhone();
        User user = userMapper.selectByPhone(phone);
        if (user != null) {
            throw new ParameterException("手机号已存在");
        }
        //保存用户信息
        User user1 = new User();
        BeanUtils.copyProperties(userRegisterDto, user1);
        //设置，密码
        user1.setPassword(DigestUtils.md5DigestAsHex(user1.getPassword().getBytes()));
        user1.setCreateTime(LocalDateTime.now());
        user1.setUpdateTime(LocalDateTime.now());
        userMapper.insert(user1);

    }

    /**
     * 用户登录
     *
     * @param userLoginDto
     * @return
     */
    public UserLoginVo login(UserLoginDto userLoginDto) {
        //判断参数是否为空
        if (userLoginDto == null) {
            throw new ParameterException("参数不能为空");
        }
        //判断手机号是否为空
        if (userLoginDto.getPhone() == null || userLoginDto.getPassword() == null) {
            throw new ParameterException("手机号或密码不能为空");
        }
        //判断手机号是否存在
        String phone = userLoginDto.getPhone();
        User user = userMapper.selectByPhone(phone);
        //判断用户是否存在
        if (user == null) {
            throw new ParameterException("用户不存在");
        }
        //验证密码
        //判断密码是否正确
        String password = userLoginDto.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());


        if (!user.getPassword().equals(password)) {
            throw new ParameterException("密码错误");
        }
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getUserId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        //返回登录成功信息
        return UserLoginVo.builder().id(user.getUserId()).token(token).build();

    }


}

